﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using RibbonEditor.Rules;
using RibbonEditor.Controls.Rules;
using RibbonEditor.Controls;

namespace RibbonEditor.Forms.Rules
{
    public partial class CreateRuleDialog : Form
    {
        XmlNode currentNode;
        TreeView globalTreeView;

        IControlRulable currentControl;

        public CreateRuleDialog(XmlNode node, TreeView mainTv)
        {
            InitializeComponent();

            this.currentNode = node;
            this.globalTreeView = mainTv;

            if (node.Name != "EnableRules" && node.Name != "DisplayRules" && node.Name != "TabDisplayRules")
            {
                this.FillControls(null);
            }
            else
            {
                RuleSelectionDialog rsDialog;

                if (node.Name == "EnableRules")
                {
                    rsDialog = new RuleSelectionDialog(RuleSelectionDialog.RuleType.EnableRule);

                    
                }
                else if (node.Name == "DisplayRules")
                {
                    rsDialog = new RuleSelectionDialog(RuleSelectionDialog.RuleType.DisplayRule);
                }
                else
                {
                    rsDialog = new RuleSelectionDialog(RuleSelectionDialog.RuleType.TabDisplayRule);
                }

                rsDialog.StartPosition = FormStartPosition.CenterParent;
                     
                if (rsDialog.ShowDialog() == DialogResult.OK)
                {
                    this.FillControls(rsDialog.SelectedRule);
                }
            }

            //this.SetToolTips();
        }

        private void FillControls(object rule)
        {
            string switchValue = rule == null ? currentNode.Name : rule.GetType().FullName.Replace(rule.GetType().Namespace + ".", "");
            
            switch (switchValue)
            {
                case "AuthenticationRule":
                    {
                        if (rule == null)
                        {
                            rule = new AuthenticationRule(currentNode);
                        }

                        AuthenticationRuleControl ctrl = new AuthenticationRuleControl((AuthenticationRule)rule);
                        this.AddControlToForm(ctrl);

                    }
                    break;
                case "CrmClientTypeRule":
                    {
                        if (rule == null)
                        {
                            rule = new CrmClientTypeRule(currentNode);
                        }

                        CrmClientTypeRuleControl ctrl = new CrmClientTypeRuleControl((CrmClientTypeRule)rule);
                        this.AddControlToForm(ctrl);

                    }
                    break;
                case "CrmOfflineAccessStateRule":
                    {
                        if (rule == null)
                        {
                            rule = new CrmOfflineAccessStateRule(currentNode);
                        }

                        CrmOfflineAccessStateRuleControl ctrl = new CrmOfflineAccessStateRuleControl((CrmOfflineAccessStateRule)rule);
                        this.AddControlToForm(ctrl);

                    }
                    break;
                case "CrmOutlookClientTypeRule":
                    {
                        if (rule == null)
                        {
                            rule = new CrmOutlookClientTypeRule(currentNode);
                        }

                        CrmOutlookClientTypeControl ctrl = new CrmOutlookClientTypeControl((CrmOutlookClientTypeRule)rule);
                        this.AddControlToForm(ctrl);

                    }
                    break;
                case "CustomRule":
                    {
                        if (rule == null)
                        {
                            rule = new CustomRule(currentNode);
                        }

                        CustomRuleControl ctrl = new CustomRuleControl((CustomRule)rule);
                        this.AddControlToForm(ctrl);

                    }
                    break;
                case "EntityPrivilegeRule":
                    {
                        if (rule == null)
                        {
                            rule = new EntityPrivilegeRule(currentNode);
                        }

                        EntityPrivilegeRuleControl ctrl = new EntityPrivilegeRuleControl((EntityPrivilegeRule)rule);
                        this.AddControlToForm(ctrl);

                    }
                    break;
                case "EntityPropertyRule":
                    {
                        if (rule == null)
                        {
                            rule = new EntityPropertyRule(currentNode);
                        }

                        EntityPropertyRuleControl ctrl = new EntityPropertyRuleControl((EntityPropertyRule)rule);
                        this.AddControlToForm(ctrl);

                    }
                    break;
                case "EntityRule":
                    {
                        if (rule == null)
                        {
                            rule = new EntityRule(currentNode);
                        }

                        EntityRuleControl ctrl = new EntityRuleControl((EntityRule)rule);
                        this.AddControlToForm(ctrl);

                    }
                    break;
                case "FormEntityContextRule":
                    {
                        if (rule == null)
                        {
                            rule = new FormEntityContextRule(currentNode);
                        }

                        FormEntityContextRuleControl ctrl = new FormEntityContextRuleControl((FormEntityContextRule)rule);
                        this.AddControlToForm(ctrl);

                    }
                    break;
                case "FormStateRule":
                    {
                        if (rule == null)
                        {
                            rule = new FormStateRule(currentNode);
                        }

                        FormStateRuleControl ctrl = new FormStateRuleControl((FormStateRule)rule);
                        this.AddControlToForm(ctrl);

                    }
                    break;
                case "MiscellaneousPrivilegeRule":
                    {
                        if (rule == null)
                        {
                            rule = new MiscellaneousPrivilegeRule(currentNode);
                        }

                        MiscellaneousPrivilegeRuleControl ctrl = new MiscellaneousPrivilegeRuleControl((MiscellaneousPrivilegeRule)rule);
                        this.AddControlToForm(ctrl);

                    }
                    break;
                case "OrganizationSettingRule":
                    {
                        if (rule == null)
                        {
                            rule = new OrganizationSettingRule(currentNode);
                        }

                        OrganizationSettingRuleControl ctrl = new OrganizationSettingRuleControl((OrganizationSettingRule)rule);
                        this.AddControlToForm(ctrl);

                    }
                    break;
                case "OutlookItemTrackingRule":
                    {
                        if (rule == null)
                        {
                            rule = new OutlookItemTrackingRule(currentNode);
                        }

                        OutlookItemTrackingRuleControl ctrl = new OutlookItemTrackingRuleControl((OutlookItemTrackingRule)rule);
                        this.AddControlToForm(ctrl);

                    }
                    break;
                case "OutlookRenderTypeRule":
                    {
                        if (rule == null)
                        {
                            rule = new OutlookRenderTypeRule(currentNode);
                        }

                        OutlookRenderTypeRuleControl ctrl = new OutlookRenderTypeRuleControl((OutlookRenderTypeRule)rule);
                        this.AddControlToForm(ctrl);

                    }
                    break;
                case "OutlookVersionRule":
                    {
                        if (rule == null)
                        {
                            rule = new OutlookVersionRule(currentNode);
                        }

                        OutlookVersionRuleControl ctrl = new OutlookVersionRuleControl((OutlookVersionRule)rule);
                        this.AddControlToForm(ctrl);

                    }
                    break;
                case "PageRule":
                    {
                        if (rule == null)
                        {
                            rule = new PageRule(currentNode);
                        }

                        PageRuleControl ctrl = new PageRuleControl((PageRule)rule);
                        this.AddControlToForm(ctrl);

                    }
                    break;
                case "RecordPrivilegeRule":
                    {
                        if (rule == null)
                        {
                            rule = new RecordPrivilegeRule(currentNode);
                        }

                        RecordPrivilegeRuleControl ctrl = new RecordPrivilegeRuleControl((RecordPrivilegeRule)rule);
                        this.AddControlToForm(ctrl);

                    }
                    break;
                case "ReferencingAttributeRequiredRule":
                    {
                        if (rule == null)
                        {
                            rule = new ReferencingAttributeRequiredRule(currentNode);
                        }

                        ReferencingAttributeRequiredRuleControl ctrl = new ReferencingAttributeRequiredRuleControl((ReferencingAttributeRequiredRule)rule);
                        this.AddControlToForm(ctrl);

                    }
                    break;
                case "RelationshipTypeRule":
                    {
                        if (rule == null)
                        {
                            rule = new RelationshipTypeRule(currentNode);
                        }

                        RelationshipTypeRuleControl ctrl = new RelationshipTypeRuleControl((RelationshipTypeRule)rule);
                        this.AddControlToForm(ctrl);

                    }
                    break;
                case "SelectionCountRule":
                    {
                        if (rule == null)
                        {
                            rule = new SelectionCountRule(currentNode);
                        }

                        SelectionCountRuleControl ctrl = new SelectionCountRuleControl((SelectionCountRule)rule);
                        this.AddControlToForm(ctrl);

                    }
                    break;
                case "SkuRule":
                    {
                        if (rule == null)
                        {
                            rule = new SkuRule(currentNode);
                        }

                        SkuRuleControl ctrl = new SkuRuleControl((SkuRule)rule);
                        this.AddControlToForm(ctrl);

                    }
                    break;
                case "ValueRule":
                    {
                        if (rule == null)
                        {
                            rule = new ValueRule(currentNode);
                        }

                        ValueRuleControl ctrl = new ValueRuleControl((ValueRule)rule);
                        this.AddControlToForm(ctrl);

                    }
                    break;
            }
        }

        private void AddControlToForm(UserControl ctrl)
        {
            ctrl.Location = new Point(7, 67);
            this.Controls.Add(ctrl);

            this.currentControl = (IControlRulable)ctrl;
        }

        private void SetToolTips()
        {
            //this.toolTip.SetToolTip(this.lblDisplayName, "Set the display name of the web resource");
            //this.toolTip.SetToolTip(this.lblName, "Set the schema name of the web resource");
            //this.toolTip.SetToolTip(this.lblFileName, "Set the path to the web resource in the customization archive");
            //this.toolTip.SetToolTip(this.lblType, "Set the type of the web resource");
        }

        private void btnValidate_Click(object sender, EventArgs e)
        {
            this.currentControl.Save();
            this.currentControl.BuildObjectXml(this.currentNode);

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
